package map.viewmodel

import android.graphics.Color
import com.baidu.mapapi.map.MapStatusUpdateFactory
import com.baidu.mapapi.model.LatLng
import com.baidu.mapapi.model.LatLngBounds
import com.baidu.mapapi.search.core.PoiInfo
import com.baidu.mapapi.search.poi.PoiDetailResult
import com.baidu.mapapi.search.poi.PoiResult
import map.ext.locationManager
import map.ext.map
import map.ext.mapManager
import map.utils.overlayutil.LineOverlay
import java.util.*
import kotlin.collections.ArrayList

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/10/25 12:02
 * @description：
 */
class PreviewViewModel : MapBaseViewModel() {


    private var lineOverlay: LineOverlay? = null

    fun previewDetailWithMe(result: PoiDetailResult) {
        val myLocation = locationManager().locationLatLng()
        val bounds = ArrayList<LatLng>().apply {
            this.add(myLocation)
            this.add(result.location)
        }
        map().setMapStatus(
            MapStatusUpdateFactory.newLatLngBounds(
                LatLngBounds.Builder().include(bounds).build()
            )
        )
        mapManager().poiOverLay().apply {
            this.setData(PoiResult().apply {
                setPoiInfo(ArrayList<PoiInfo>().apply {
                    add(PoiInfo().apply {
                        setLocation(result.location)
                        setUid(result.uid)
                        setAddress(result.address)
                        setName(result.name)
                        setPhoneNum(result.telephone)
                        setTag(result.tag)
                    })
                })
            })
            this.addToMap()
        }
        mapManager().zoomOut()
    }

    /**
     * 预览爱者地址
     * */
    fun previewLover(lover: LatLng) {
        mapManager().locationManager().requestCurrentLocationByCall {
            val myLocation = it
            val bounds = ArrayList<LatLng>().apply {
                this.add(myLocation)
                this.add(lover)
            }
            map().setMapStatus(
                MapStatusUpdateFactory.newLatLngBounds(
                    LatLngBounds.Builder().include(bounds).build()
                )
            )

            mapManager().loverOverLay().apply {
                bindLover(myLocation, lover)
                this.addToMap()
            }

            mapManager().zoomBy(bounds)
        }
    }

    fun preLineToMe(point: LatLng, color: Int = Color.RED) {
        val self = locationManager().locationLatLng()
        mapManager().lineOverlay().apply {
            lineOverlay = this
            bindData(listOf(point, self), color)
            addToMap()
        }
        mapManager().zoomBy(listOf(self,point))
    }

    fun removeLineToMe() {
        lineOverlay?.removeFromMap()
    }


    fun clear() {
        map().clear()
    }

    fun preViewSinglePoi(latLng: LatLng) {
        map().setMapStatus(
            MapStatusUpdateFactory.newLatLngBounds(
                LatLngBounds.Builder().include(Collections.singletonList(latLng)).build()
            )
        )
        mapManager().zoomBy(16f)
    }
}